<template>
  <el-drawer
    v-model="drawerVisible"
    title="商品详情"
    direction="rtl"
    size="500px"
    :destroy-on-close="true"
    :close-on-click-modal="true"
  >
    <el-form class="product-detail-form" label-width="100px" v-loading="loading">
      <el-form-item label="商品编码">
        <span>{{ productDetail.barCode || productDetail.id }}</span>
      </el-form-item>
      <el-form-item label="商品名称">
        <span>{{ productDetail.name }}</span>
      </el-form-item>
      <el-form-item label="商品主图" v-if="productDetail.logo">
        <el-image
          style="width: 120px; height: 120px"
          :src="productDetail.logo"
          fit="cover"
          :preview-src-list="[productDetail.logo]"
        ></el-image>
      </el-form-item>
      <el-form-item label="商品分类">
        <span>{{ productDetail.catalogName }}</span>
      </el-form-item>
      <el-form-item label="商品分组">
        <span>{{ productDetail.productGroupName }}</span>
      </el-form-item>
      <el-form-item label="库存">
        <span>{{ productDetail.stockNum }}</span>
      </el-form-item>
      <el-form-item label="价格">
        <span class="price">¥{{ productDetail.price }}</span>
      </el-form-item>
      <el-form-item label="折扣">
        <span>{{ productDetail.discount }}</span>
      </el-form-item>
      <el-form-item label="上架状态">
        <el-tag :type="productDetail.state === 1 ? 'success' : 'info'">
          {{ productDetail.state === 1 ? '已上架' : '已下架' }}
        </el-tag>
      </el-form-item>
      <el-form-item label="创建时间" v-if="productDetail.createTime">
        <span>{{ formatDate(productDetail.createTime) }}</span>
      </el-form-item>
      <el-form-item label="最后更新" v-if="productDetail.lastUpdate">
        <span>{{ formatDate(productDetail.lastUpdate) }}</span>
      </el-form-item>
      <el-form-item label="商品简介">
        <div class="product-summary">{{ productDetail.summary }}</div>
      </el-form-item>
    </el-form>
    
    <template #footer>
      <div style="flex: auto">
        <el-button @click="closeDrawer">关闭</el-button>
        <el-button type="primary" @click="editProduct" v-if="canEdit">编辑</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script setup>
import { ref, watch } from 'vue';
import { ElMessage } from 'element-plus';
import http from '@/utils/request';

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  productId: {
    type: [String, Number],
    default: ''
  },
  canEdit: {
    type: Boolean,
    default: true
  }
});

const emit = defineEmits(['update:visible', 'edit']);

const drawerVisible = ref(false);
const loading = ref(false);
const productDetail = ref({});

// 监听props.visible变化
watch(() => props.visible, (val) => {
  drawerVisible.value = val;
  if (val && props.productId) {
    loadProductDetail();
  }
});

// 监听抽屉可见性变化
watch(() => drawerVisible.value, (val) => {
  emit('update:visible', val);
});

// 加载商品详情
const loadProductDetail = async () => {
  if (!props.productId) return;
  
  loading.value = true;
  try {
    const data = { id: props.productId };
    const res = await http.post("/product/view", data);
    if (res.code === 200) {
      productDetail.value = res.data || {};
    } else {
      ElMessage.error(res.msg || '获取商品详情失败');
    }
  } catch (error) {
    console.error('获取商品详情时出错:', error);
    ElMessage.error('获取商品详情失败');
  } finally {
    loading.value = false;
  }
};

// 关闭抽屉
const closeDrawer = () => {
  drawerVisible.value = false;
};

// 编辑商品
const editProduct = () => {
  emit('edit', productDetail.value);
  closeDrawer();
};

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '';
  const date = new Date(dateString);
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  });
};
</script>

<style scoped>
.product-detail-form {
  padding: 20px;
}

.price {
  color: #f56c6c;
  font-weight: bold;
  font-size: 16px;
}

.product-summary {
  white-space: pre-wrap;
  line-height: 1.6;
  color: #606266;
  max-height: 200px;
  overflow-y: auto;
  padding: 8px;
  background-color: #f8f8f8;
  border-radius: 4px;
}
</style> 